package com.sskj.extract;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;

import javax.swing.*;

public class MainDialog extends JDialog {
    private JPanel contentPane;
    private JButton backButton;
    private JButton extractButton;
    private JButton clearButton;
    private JList list1;
    Project project;


    public MainDialog(Project project) {
        this.project = project;
        setModal(false);
        setLocation(100, 100);
        setBounds(0, 0, 300, 250);
        setContentPane(contentPane);
        list1.setListData(Config.list.toArray());

        backButton.addActionListener(actionEvent -> {
            try {
                Object[] selectedValues = list1.getSelectedValues();
                Config.map.clear();
                for (Object selectedValue : selectedValues) {
                    Config.getMap().put(Config.basePath + (String) selectedValue, (String) selectedValue + "");
                }
                Back.back();
                Messages.showMessageDialog(project, "回返成功", "提醒", Messages.getInformationIcon());

            } catch (Exception e) {
                e.printStackTrace();
                Messages.showMessageDialog(project, e.toString(), "回返失败", Messages.getInformationIcon());
            }
        });
        extractButton.addActionListener(actionEvent -> {
            try {
                Object[] selectedValues = list1.getSelectedValues();
                Config.map.clear();
                for (Object selectedValue : selectedValues) {
                    Config.getMap().put(Config.basePath + (String) selectedValue, (String) selectedValue + "");
                }
                Extract.extract();
                Messages.showMessageDialog(project, "抽取成功", "提醒", Messages.getInformationIcon());

            } catch (Exception e) {
                e.printStackTrace();
                Messages.showMessageDialog(project, e.toString(), "抽取失败", Messages.getInformationIcon());
            }
        });
        clearButton.addActionListener(actionEvent -> {
            try {
                Object[] selectedValues = list1.getSelectedValues();
                Config.map.clear();
                for (Object selectedValue : selectedValues) {
                    Config.getMap().put(Config.basePath + (String) selectedValue, (String) selectedValue + "");
                }
                Clear.clear();
                Messages.showMessageDialog(project, "清理成功", "提醒", Messages.getInformationIcon());

            } catch (Exception e) {
                e.printStackTrace();
                Messages.showMessageDialog(project, e.toString(), "清理失败", Messages.getInformationIcon());
            }
        });


    }


}
